package org.example.springboot.dao.impl;

import org.example.springboot.dao.OrderDAO;
import org.example.springboot.entity.Order;
import org.example.springboot.util.JDBCUtils;

import java.util.List;

public class OrderDAOImpl implements OrderDAO {

    @Override
    public List<Order> orderLimit(int start, int rows) {
        return JDBCUtils.executeQuery(Order.class,"select * from s_order limit ?,?",
                start,rows);
    }

    @Override
    public Integer selectorderCount() {
        return JDBCUtils.executeCount("select count(*) from s_order ");
    }

    @Override
    public List<Order> setAll() {
        List<Order> orders = JDBCUtils.executeQuery(Order.class,"select * from s_order");
        return orders;
    }

    @Override
    public Order selectById(String id) {
        List<Order> orders = JDBCUtils.executeQuery(Order.class,"select * from s_order where id = ?",id);
        if (orders.isEmpty()){
            return null;
        }else {
            return orders.get(0);}
    }

    @Override
    public boolean inser(Order order) {
        int rows = JDBCUtils.executeUpdate("insert into s_order(id,create_time,receiving_address,user_id)values(?,?,?,?)",
                order.getId(),order.getCreate_time(),order.getReceiving_address(),order.getUser_id());
        return rows > 0;
    }

    @Override
    public boolean delete(String id) {
        int rows = JDBCUtils.executeUpdate("delete from s_order where id = ?",id);
        return rows > 0;
    }

    @Override
    public boolean update(Order order) {
        int rows = JDBCUtils.executeUpdate("update s_order set create_time = ?,receiving_address = ?,user_id = ? where id = ?",
                order.getCreate_time(),order.getReceiving_address(),order.getUser_id(),order.getId());
        return rows > 0;
    }

}
